/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.asn1.naming;

import org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProvider;
import org.eclipse.xtext.naming.QualifiedName;

import com.google.inject.Inject;

import de.thwildau.tm.moses.asn1.asn1.ContainerType;
import de.thwildau.tm.moses.asn1.asn1.EnumeratedType;
import de.thwildau.tm.moses.asn1.asn1.ModuleDefinition;
import de.thwildau.tm.moses.asn1.asn1.Type;
import de.thwildau.tm.moses.asn1.util.Asn1ElementFinder;
import de.thwildau.tm.moses.asn1.util.NamesConverter;

/**
 * A QualifiedNameProvider for ASN.1 modules.
 * 
 * @author Thomas Kistel
 * @version $Revision: 763 $
 */
public class Asn1QualifiedNameProvider extends DefaultDeclarativeQualifiedNameProvider {
    
    @Inject
    private Asn1ElementFinder finder;
    
    @Inject
    private NamesConverter converter;
    
    
    QualifiedName qualifiedName(ContainerType ele) {
        return computeQualifiedName(ele);
    }
    
    QualifiedName qualifiedName(EnumeratedType ele) {
        return computeQualifiedName(ele);
    }
    
    /**
     * Generic method to compute the qualified name for a type element. However
     * the type element must be a defined, tagged or primitive type.
     */
    private QualifiedName computeQualifiedName(Type ele) {
        String name = finder.assignmentOf(ele).getName();
        ModuleDefinition moduleDefinition = finder.getModuleDefinitionOf(ele);
        QualifiedName fqn = converter.toPackageName(moduleDefinition);
        QualifiedName className = converter.toClassName(name);
        fqn = fqn.append(className);
        return fqn;
    }
}
